define(function () {

    var self;
    var requestArgs;


    var Wdbj = app.Module.extend({

        init: function () {
            self = this;
            self.mData = {
                orderSet: {},
                form: {
                    MMRT007: '',
                    MMRT009: '',
                    MMRT010: '',
                    MMRT011: ''
                }
            };
            self.searchData = {
                keyword: ''
            };
            self._super({
                _tag: 'p_Wdbj', // key, 对象放进pageModule
                _refreshAutoInit: true, //自动加载刷新控件
                _readyBefore:function () {
                    requestArgs = app.pageParameters['wdbj_page'];
                }
            }, win);
        },
        _search:function(data){
            self.searchData.keyword = data.keyword.trim();
            var goods = self.mData.orderSet['scroll'];
            goods.page = 0;
            self.getData(goods);
        },
        getData:function(goods){
            console.log(goods);
            app.ajax({
                xurl: "api/wx/wdbj/getPartListData",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    keyword: self.searchData.keyword
                },
                cb: function (result) {
                    if (result.ok) {
                        if (goods.page == 0) {
                            goods.data.removeAll();
                        }
                        var data = result.data;
                        for (var i = 0; i < data.data.length; i++) {
                            data.data[i].MMRH004 = 1;
                            goods.data.push(data.data[i]);
                        }

                        var refresh = goods.refresh;
                        if (goods.page == 0) {
                            refresh.endPulldownToRefresh();
                            if (result.data.data.length == 0) {
                                var msg = '无备件...';
                                app.showNoData(goods._tip, msg);
                                refresh.disablePullupToRefresh();
                            } else {
                                app.hideNoData(goods._tip);
                                refresh.refresh(true);
                            }
                        }
                        goods.page += 1;
                        refresh.endPullupToRefresh(result.data.data.length < goods.limit);

                    }

                }
            });
        },
        _ready:function(){
            Vue.component('wdbj-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array
                },
                methods: {
                    minus: function (val) {
                        if (val.MMRH004 > 1) {
                            val.MMRH004--;
                        }
                    },
                    plus: function (val) {
                        if (val.MMRS008 > val.MMRH004) {
                            val.MMRH004++;
                        }
                    }
                }
            });

            var refreshData = function () {
                var goods = self.mData.orderSet['scroll'];
                if (!goods) {
                    goods = self.mData.orderSet['scroll'] = {
                        page: 1,
                        limit: 15,
                        data: [],
                        start: function () {
                            return this.page * this.limit;
                        }
                    };

                    new Vue({
                        el: doc.getElementById('scroll'),
                        data: {
                            items: goods.data
                        }
                    });

                    goods._tip = $('#scroll');
                    goods.refresh = mui('#scroll').pullRefresh({
                        down: {
                            callback: function () {
                                goods.page = 0;
                                self.getData(goods);
                            },
                            auto: true
                        }, up: {
                            callback: function () {
                                self.getData(goods);
                            }
                        }
                    });
                }
            };

            refreshData(0);

            app.taps.call(win, [
                {
                    o: 'btn_bjsq',
                    cb: function (e) {
                        var goods = self.mData.orderSet['scroll'],
                            checkArr = [];
                        for (var i = 0; i < goods.data.length; i++) {
                            if (goods.data[i].checked == true) {
                                goods.data[i].MMRH002 = '1';// 默认收费
                                checkArr.push(app.cloneJSON(goods.data[i]));
                            }
                        }
                        var msg = requestArgs.cb(checkArr);
                        if (msg == 1) {
                            mui.back();
                        } else {
                            app.alert({
                                msg: '配件：' + msg + ',已经添加过了'
                            });
                        }

                    }
                }
            ]);

        },
        _lazy: function () {
            this._super();
        }
    });

    return new Wdbj();

});
